package com.leha.forum.lehare.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leha.forum.lehare.dto.PostEditDTO;
import com.leha.forum.lehare.entity.Post;
import com.leha.forum.lehare.response.Result;
import com.leha.forum.lehare.service.PostService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

/**
 * @author lovelycat
 * @version 1.0
 * @since 2025-04-15 22:48
 */
@RestController
@RequestMapping("/post")
public class PostController {
    @Resource
    private PostService postService;

    @GetMapping("/list")
    public Result<?> getPosts(Long page) {
        return Result.success("", postService.getPosts(page, 20L));
    }

    @GetMapping("/listLatest")
    public Result<?> getLatestPosts(Long page) {
        return Result.success("", postService.getPosts(page, 20L, () -> new QueryWrapper<Post>().orderByDesc("created_at")));
    }

    @GetMapping("/listStar")
    public Result<?> getStarPosts(Long page) {
        return Result.success("", postService.getPosts(page, 20L, () -> new QueryWrapper<Post>().eq("is_star", true).orderByDesc("created_at")));
    }

    @GetMapping("/listTop")
    public Result<?> getTopPosts(Long page) {
        return Result.success("", postService.getPosts(page, 20L, () -> new QueryWrapper<Post>().eq("is_star", true).orderByDesc("created_at")));
    }

    @GetMapping("/listHot")
    public Result<?> getHotPosts(Long page) {
        return Result.success(
                "",
                postService.getPosts(
                        page,
                        20L,
                        () -> new QueryWrapper<Post>().orderByDesc("view_count")
                )
        );
    }

    @GetMapping("/listByCat")
    public Result<?> getPostsByCat(Long page, Long catId) {
        return Result.success(
                "",
                postService.getPosts(
                        page,
                        20L,
                        () -> new QueryWrapper<Post>()
                                .eq("category_id", catId)
                                .orderByDesc("created_at")
                )
        );
    }

    @PostMapping("/publish")
    public Result<?> publishPost(Principal principal, @RequestBody PostEditDTO dto) {
        if (postService.publishNewPost(Long.valueOf(principal.getName()), dto)) {
            return Result.success("发布成功", null);
        } else {
            return Result.failed(401, "发布失败");
        }
    }

    @GetMapping("/details")
    public Result<?> getPostDetails(Principal principal, Long postId) {
        return Result.success("", postService.getById(postId));
    }

    @PostMapping("/setTop")
    public Result<?> setPostTop(Long postId, Boolean value) {
        if (postService.setTop(postId, value)) {
            return Result.success("操作成功", null);
        } else {
            return Result.failed(401, "操作失败");
        }
    }

    @PostMapping("/setStar")
    public Result<?> setPostStar(Long postId, Boolean value) {
        if (postService.setStar(postId, value)) {
            return Result.success("操作成功", null);
        } else {
            return Result.failed(401, "操作失败");
        }
    }

    @PostMapping("/increaseViewCount")
    public Result<?> increaseViewCount(Long postId) {
        if (postService.increaseViewCount(postId)) {
            return Result.success("y", null);
        } else {
            return Result.failed(401, "n");
        }
    }

    @PostMapping("/delete")
    public Result<?> deletePost(Long postId) {
        if (postService.removeById(postId)) {
            return Result.success("删除成功", null);
        } else {
            return Result.failed(401, "删除失败");
        }
    }
}
